package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public  class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private DestinationMapper destinationMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Destination record) {
        destinationMapper.insert(record);
    }

    @Override
    public Destination selectByPrimaryKey(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Destination> queryForList(DestinationQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> list = destinationMapper.queryForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void updateByPrimaryKey(Destination record) {
        destinationMapper.updateByPrimaryKey(record);
    }

    @Override
    public void changeHot(Long id, Integer hot) {
        destinationMapper.updateHot(id,hot);
    }

    @Override
    public List<Destination> listToasts(Long id) {
        List<Destination> list = new ArrayList<>();
        //1.查询改id对应的目的地
       Destination dest = destinationMapper.selectByPrimaryKey(id);
        list.add(dest);
        //2.取出该目的地的父级
        while(dest.getParent()!=null){
            dest = destinationMapper.selectByPrimaryKey(dest.getParent().getId());
            list.add(dest);
        }
      //  3.反转数组
        Collections.reverse(list);
        return list;
    }

    @Override//查询目的地
    public List<Destination> listDestByRegionId(Long rid) {
        Region region = regionMapper.selectByPrimaryKey(rid);
        Long[] rids = region.getRefIds();
        ArrayList<Destination> list = new ArrayList<>();
        for (int i = 0; i < rids.length; i++) {
            Destination destination = destinationMapper.selectByPrimaryKey(rids[i]);
            if (destination!= null){
                list.add(destination);
            }
        }
        return list;
    }

    @Override
    public List<Destination> selectAll() {
        return destinationMapper.selectAll();
    }

    @Override
    public List<Destination> listHotDestByRegionId(Long regionId) {
        //区域的id大于0 查询国家
        if(regionId>0){
            return destinationMapper.listHotDestByRegionId(regionId);
        }
        //区域id=-1 的国内的省分
        return destinationMapper.selectProvinces();
    }

    @Override//查询国家
    public Destination getCountry(Long destId) {
        List<Destination> list = listToasts(destId);
        return list.get(0);
    }

    @Override//查询省份
    public Destination getProvince(Long destId) {
        List<Destination> list = listToasts(destId);
        return list.size()>1? list.get(1):null;
    }
}
